package Practice;

import com.sun.source.tree.IfTree;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyJFrame extends JFrame {

    private MyTest name = new MyTest("姓名",150,100,this);
    private MyTest sex = new MyTest("性别",150,150,this);
    private MyTest birthday = new MyTest("生日",150,200,this);
    private MyTest phNum = new MyTest("电话",150,250,this);
    private MyTest salary = new MyTest("工资",150,300,this);

    public MyJFrame(){
        this.setLayout(null);

        this.init();

        this.setSize(600,700);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
    }

    private void init(){
        MyButton addButton = new MyButton("添加",250,500,this);
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addOption();
            }
        });
    }

    private String check(){
        String nameCheck = "[\u4e00-\u9fa5a-zA-Z]{2,}";
        String sexCheck = "[男女]";
        String birthCheck = "[0-9]{4}-0[1-9]|1[0-2]{2}-(0-2)[0-9]||3[0-1]";
        String phNumCheck = "1[3578][0-9]{9}";
        String salaryCheck = "1[0-9]{3}";

        String errorInfo = "";

       if (name.getText().matches(nameCheck)==false){
           errorInfo += "姓名必须为两位以上的字母或汉字"+"\n";
       }
       if (sex.getText().matches(sexCheck)==false){
           errorInfo += "性别只能是男或女"+"\n";
       }
       if (birthday.getText().matches(birthCheck)==false){
           errorInfo += "生日格式错误"+"\n";
       }
       if (phNum.getText().matches(phNumCheck)==false){
           errorInfo += "电话号码只能是13、15、17、18开头的十一位数"+"\n";
       }
       if (salary.getText().matches(salaryCheck)==false){
           errorInfo += "工资格式有误"+"\n";
       }
        return errorInfo;
    }

    private void addOption(){
        String errorInfo = this.check();
        if (errorInfo.length()!=0){
            JOptionPane.showMessageDialog(null,errorInfo);
        }else {
            JOptionPane.showMessageDialog(null,"添加成功");
        }
    }


    public static void main(String[] args) {
        new MyJFrame();
    }
}
